package cn.fanqinghong.org.service.impl;

import cn.fanqinghong.basic.exception.BusinessException;
import cn.fanqinghong.basic.query.BaseQuery;
import cn.fanqinghong.basic.util.BaiduAuditUtils;
import cn.fanqinghong.basic.util.MailUtil;
import cn.fanqinghong.basic.query.PageList;
import cn.fanqinghong.org.domain.Employee;
import cn.fanqinghong.org.domain.Shop;
import cn.fanqinghong.org.domain.ShopAuditLog;
import cn.fanqinghong.org.mapper.EmployeeMapper;
import cn.fanqinghong.org.mapper.ShopAuditLogMapper;
import cn.fanqinghong.org.mapper.ShopMapper;
import cn.fanqinghong.org.service.IEmployeeService;
import cn.fanqinghong.org.service.IShopService;
import cn.fanqinghong.basic.service.impl.BaseServiceImpl;
import cn.fanqinghong.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fanqinghong
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 店铺入驻流程
     * 一，参数是否为空判断
     * 二，合法校验   @TODO 使用百度AI智能审核接口
     * 三，判断是否曾经注册过
     *        1，判断店铺是否已经存在
     *        2，店员是否存在
     *        2.1 店员已经存在并且已经绑定了店铺
     *        2.2 店员已经注册但是没有绑定店铺
     *四，保存数据
     * @param shop
     */
    @Override
    public void selectment(Shop shop) {
        //得到传入数据的Admin的数据
        Employee admin = shop.getAdmin();
        //* 店铺入驻流程
        //* 一，参数是否为空判断
        if (StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(admin)
        ) {
            throw new BusinessException("店铺不能为空");
        }
        //* 二，合法校验    使用百度AI智能审核接口
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("小伙子，千万不要搞了，小心我叫艾同学来捅你屁股哦!!!!");
        }
        if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208/"+shop.getLogo())){
            throw new BusinessException("小伙子，千万不要搞了，这个图片违规了哦!!!!");
        }
        //* 三，判断是否曾经注册过
        //* 1，判断店铺是否已经存在
        //通过name查询店铺数据
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if (shopTmp != null){
            throw new BusinessException("店铺已经存在，请登录");
        }
        //* 2，店员是否存在
        Employee empTmp = employeeMapper.lodaByUsername(admin.getUsername());
        //* 2.1 店员已经存在并且已经绑定了店铺
        if (empTmp != null && empTmp.getShop_id() != null){
            throw new BusinessException("该用户已经绑定了店铺，请重写绑定");
        }
        //* 2.2 店员已经注册但是没有绑定店铺
        if (empTmp == null){
            //把传入数据的admin存入员工表中
            employeeService.save(admin);//这里需要返回自增id
            //把admin赋值给查询到的店员数据
            empTmp = admin;
        }
        //四，保存数据
        //设置Shop的员工id（adminId）
        shop.setAdminId(empTmp.getId());
        //在店铺表中保存数据，并且需要返回自增id
        shopMapper.save(shop);
        //设置Emplyee中的店铺id（shop_id）
        empTmp.setShop_id(shop.getId());
        //执行方法修改员工表中的店铺id（shop_id)
        employeeMapper.update(empTmp);
    }

    /**
     * 审核通过
     * @param log
     */
    @Override
    public void pass(ShopAuditLog log) {

            //1.修改店铺状态为---审核通过  @TODO   你们做
            Shop shop = shopMapper.loadById(log.getShopId());
            shop.setState(1);
            shopMapper.update(shop);
            //2.记录审核通过日志
            log.setAuditId(334L);//@TODO
            log.setState(1);
            shopAuditLogMapper.save(log);
            //3.发送激活邮件
            MailUtil.sendEasyMailPass(log);
//        Employee employee = employeeMapper.loadByShopId(log.getShopId());
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom("1412644233@qq.com");
//        message.setTo(employee.getEmail());
//        message.setSubject("宠物乐园-店铺入驻激活请求");
//        message.setText("亲，您的店铺信息已经通过审核，请尽快完成激活并使用：" +
//                "激活链接：http://localhost:8080/shop/activation/"+log.getShopId());
//        javaMailSender.send(message);

    }


    /**
     * 申请驳回
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) {

            System.err.println(log);
            //1.修改店铺状态为---审核通过  @TODO   你们做
            Shop shop = shopMapper.loadById(log.getShopId());
            shop.setState(-1);
            shopMapper.update(shop);
            //2.记录审核未通过日志
            log.setAuditId(334L);//@TODO
            log.setState(-1);
            shopAuditLogMapper.save(log);
            //3.发送修改邮件
            System.err.println(log);
            MailUtil.sendEasyMailReject(log);
        /*Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1412644233@qq.com");
        message.setTo(employee.getEmail());
        message.setSubject("宠物乐园-店铺入驻激活请求");
        message.setText("亲，您的店铺信息已经驳回，请重新填写内容，修改链接：" +
                "http://localhost:8081/#/register2?shopId="+log.getShopId());
        javaMailSender.send(message);*/

    }

    /**
     * 批量新增
     * @param shops
     */
    @Override
    public void savas(List<Shop> shops) {
        shopMapper.savas(shops);
    }

    /**
     * 查询echarts
     * @return
     */
    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }

    /**
     * 高级查询+分页查询
     * @param query
     * @return
     */
    @Override
    public PageList<Shop> queryPage(BaseQuery query) {
        //查询分页总条数
        Long total = shopMapper.queryCount(query);
        //查询当前页数据
        List<Shop> rows = shopMapper.queryData(query);
        for (Shop row : rows) {
            Long shopId = row.getId();
            List<ShopAuditLog> audit_id = shopAuditLogMapper.findAudit_id(shopId);
            row.setAuditLogs(audit_id);
        }

        return new PageList<>(total,rows);
    }

}
